package com.kaiyidiannao.activity.convenience_check;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.ConvenienceCheckModel;
import com.kaiyidiannao.view.ActionBarView;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.AbstractWheelTextAdapter;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by guanzhe on 14-4-16.
 */
public class ConvenienceCheck extends BaseActionBarActivity {
    private WheelView convenienceWheel;
    private ListView convenienceCheckLv;
    private List<List<ConvenienceCheckModel>> lists = new ArrayList<List<ConvenienceCheckModel>>();
    private int wheelTag = 0;//用来记录当前界面下方的轮子控件在哪一行，默认值为0
    ContentAdapter contentAdapter = new ContentAdapter();
//    ScrollView convenienceCheckSv;
    private boolean flag = true; // 顶部title右侧的按钮点击表示

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.convenience_check);
        initLists();
        findActionBar();
        initView();
    }
    private void findActionBar() {
        actionBarView = (ActionBarView) findViewById(R.id.actionbar);
        actionBarView.setTitle("便民查");
        actionBarView.button_right_icon2.setImageResource(R.drawable.convenience_check);
        actionBarView.button_right_icon2.setVisibility(View.VISIBLE);
        actionBarView.button_right_icon2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag) {
                    flag = false;
                    convenienceWheel.setVisibility(View.GONE);
                } else {
                    flag = true;
                    convenienceWheel.setVisibility(View.VISIBLE);
                }
            }
        });
    }
    public void initView(){
        convenienceWheel = (WheelView) findViewById(R.id.convenienceWheel);
        convenienceWheel.setVisibleItems(5);
        convenienceWheel.setViewAdapter(new TypeAdapter(this));
//        convenienceCheckSv = (ScrollView) findViewById(R.id.convenienceCheckSv);
        convenienceCheckLv = (ListView) findViewById(R.id.convenienceCheckLv);
        convenienceCheckLv.setAdapter(contentAdapter);
//        convenienceCheckSv.smoothScrollTo(0,0);
        convenienceWheel.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                wheelTag = convenienceWheel.getCurrentItem();
                updateListView();
            }
        });
    }


    /**
     *初始化lists
     */
    public void initLists(){
        //实例化工厂对象
        XmlPullParserFactory factory = null;
        ConvenienceCheckModel convenienceCheckModel = null;
        List<ConvenienceCheckModel> list = new ArrayList<ConvenienceCheckModel>();
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xmlPullParser = factory.newPullParser();
            InputStream inputStream = this.getResources().openRawResource(R.raw.convenience_check_contents);
            xmlPullParser.setInput(inputStream,"UTF-8");
            int eventType = xmlPullParser.getEventType();
            //如果不读到end标签就继续循环
            while(eventType != xmlPullParser.END_DOCUMENT){
                String tag = xmlPullParser.getName();
                //如果是start标签
                switch (eventType){
                    case XmlPullParser.START_TAG:
                        if(tag.equals("name")){
                            convenienceCheckModel = new ConvenienceCheckModel();
                            convenienceCheckModel.setName(xmlPullParser.nextText());
                        }else if(tag.equals("number")){
                            convenienceCheckModel.setNumber(xmlPullParser.nextText());
                            list.add(convenienceCheckModel);//2个属性都已经添加，将其加入list
                        }
                        break;
                    //如果是end标签
                    case XmlPullParser.END_TAG:
                        //如果end标签是categories按逻辑加入lists清除子list内容
                        if(tag.equals("categories")){
                            lists.add(list);
                            list = new ArrayList<ConvenienceCheckModel>();
//                            list.clear();
                        }
                        break;
                }
                eventType = xmlPullParser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 上方listView的adapter
     */
    private class ContentAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return lists.get(wheelTag).size();
        }

        @Override
        public Object getItem(int i) {
            return lists.get(wheelTag).get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup viewGroup) {
            ViewHolder viewHolder = null;
            if(convertView == null){
                viewHolder = new ViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.convenience_check_list_item,null);
                viewHolder.lvItemTopTv = (TextView) convertView.findViewById(R.id.lvItemTopTv);
                viewHolder.lvItemBottomTv = (TextView) convertView.findViewById(R.id.lvItemBottomTv);
                viewHolder.lvItemLeftIv = (ImageView) convertView.findViewById(R.id.lvItemLeftIv);
                viewHolder.lvItemRightIv = (ImageView) convertView.findViewById(R.id.lvItemRightIv);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            convertView.setClickable(true);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + lists.get(wheelTag).get(position).getNumber()));
                    startActivity(intent);
                }
            });
            viewHolder.lvItemTopTv.setText(lists.get(wheelTag).get(position).getName());
            viewHolder.lvItemBottomTv.setText(lists.get(wheelTag).get(position).getNumber());
            return convertView;
        }
    }
    private class ViewHolder{
        TextView lvItemBottomTv,lvItemTopTv;
        ImageView lvItemLeftIv,lvItemRightIv;
    }
    private class TypeAdapter extends AbstractWheelTextAdapter {
        private String types [] = new String []{"公众服务、紧急电话","快递服务","汽车服务","银行服务","监督投诉、抢修举报","企业客服","运营商服务","保险公司服务","大学号码","党政、公益团体"};

        protected TypeAdapter(Context context) {
            super(context);
        }

        @Override
        public int getItemsCount() {
            return types.length;
        }

        @Override
        protected CharSequence getItemText(int index) {
            return types[index];
        }

        @Override
        public View getItem(int index, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = getLayoutInflater().inflate(R.layout.convenience_lvitem,null);
            }
            TextView textView = (TextView) convertView.findViewById(R.id.convenienceLvItemTv);
            textView.setText(types[index]);
            return super.getItem(index, convertView, parent);
        }

    }

    /**
     * 更新listView视图
     */
    private void updateListView(){
//        convenienceCheckSv.smoothScrollTo(0,0);
        contentAdapter.notifyDataSetChanged();
    }


}
